let Sequelize = require("sequelize");

module.exports = function(sequelize, DataTypes) {
	let Order = sequelize.define("Order", {
        id: { type: Sequelize.INTEGER, primaryKey: true,autoIncrement: true },     
        order_sn : { type: Sequelize.STRING},
        order_type : { type: Sequelize.INTEGER(2)},//默认1
		province : { type: Sequelize.STRING},
        country : { type: Sequelize.STRING},
        city : { type: Sequelize.STRING},
        domain : { type: Sequelize.STRING},
        address : { type: Sequelize.STRING},
        total_price: { type: Sequelize.BIGINT},//单位分
        goods_price:{ type: Sequelize.BIGINT},//单位分
        shipping_fee :{ type: Sequelize.BIGINT},//单位分
        order_status : { type: Sequelize.INTEGER(1)},//0，正常，1，取消
        shipping_status : { type: Sequelize.INTEGER(1)},//0，未发，1，已发
        pay_status : { type: Sequelize.INTEGER(1)},//0，未，1，正在，2，已
        pay_type : { type: Sequelize.INTEGER(2)},//默认1，
        consignee: { type: Sequelize.STRING},
        mobile: { type: Sequelize.STRING},
        transNoSys: { type: Sequelize.STRING},
        transNoMch: { type: Sequelize.STRING},
        postscript: { type: Sequelize.STRING},//客户留言
        product_ids: { type: Sequelize.STRING},
        remark: { type: Sequelize.STRING},
        extra: { type: Sequelize.STRING},
        amount:{ type: Sequelize.INTEGER},
        pay_time:{type:Sequelize.DATE},
        uid : {type: Sequelize.INTEGER,
			references: {
				model: 'User',
				key: 'id',
			}
		}
	}, {
		'tableName': 'base_order',
		'comment': '订单表',
		'underscored': true,
		'timestamps': true,
		'paranoid': true,
		'createdAt' : 'created_at',
        'updatedAt' : 'updated_at',
        'deletedAt': 'deleted_at',
		classMethods: {
		    associate: function(models) {
                Order.belongsTo(models.User, {
                    onDelete: "CASCADE",
                    foreignKey: {
                        name : 'uid',
                        allowNull: false
                    }
                });
                models.User.hasMany(Order,{foreignKey : 'uid',targetKey:'id'})
            }
		},
		instanceMethods: {
			toJSON: function () {
			    let values = Object.assign({}, this.get());
			    return values;
			}
		}
	});
	return Order;
};
